package message;

import model.RoomInfo;
import socket.ChessServer;

/**
 * 退出房间报文.
 *
 * @author 张紘溢
 */
public class OutRoomMessage extends BaseMessage {
    private int roomId;
    private boolean isLeft;

    public OutRoomMessage(int roomId, boolean isLeft) {
        this.roomId = roomId;
        this.isLeft = isLeft;
    }

    @Override
    public void sendToServer() {
        RoomInfo roomInfo = ChessServer.getInstance().getRooms().get(roomId);
        if (roomInfo.isLeftReady() && roomInfo.isRightReady()) {
            roomInfo.setRightReady(false);
            roomInfo.setLeftReady(false);
        }
        if (roomInfo.getStatus() == RoomInfo.PLAYING) {
            roomInfo.setStatus(RoomInfo.WAIT);
        } else {
            roomInfo.setStatus(RoomInfo.NONE);
        }
        if (!isLeft) {
            System.out.println("右边玩家设置null");
            roomInfo.setRightPlayer(null);
        }
        if (isLeft) {
            System.out.println("左边玩家设置null");
            roomInfo.setLeftPlayer(null);
        }
        ChessServer.getInstance().getRooms().set(roomId, roomInfo);
        ServerRoomListMessage serverRoomListMessage = new ServerRoomListMessage(ChessServer.getInstance().getRooms());
        ChessServer.getInstance().sendMsgToAll(serverRoomListMessage);
        ServerRoomPlayerMessage serverRoomPlayerMessage = new ServerRoomPlayerMessage(roomInfo);
        ChessServer.getInstance().sendMsgToAll(serverRoomPlayerMessage);
    }

    @Override
    public void selfExecute() {

    }
}
